class login{
    constructor(){
        //获取
        this.us = this.$('#us');
        this.ps = this.$('#ps');
        this.ipt_1 = this.$('.ipt_1');
        this.arr = [false,false];
        //
        this.key1 = this.$('#key1');
        this.key2 = this.$('#key2');
        this.key3 = this.$('#key3');
        //
        this.div1 = this.$('#div1');
        this.div2 = this.$('#div2');
        this.div3 = this.$('#div3');
        // 添加事件
        this.add();
    }
        

      add(){
        let that = this;
        this.us.onblur = function(){
            var re = /0?(13|14|15|17|18|19)[0-9]{9}/;
            var us = this.value;
            if(re.test(us)){
                this.style.borderColor = '';
                 that.arr[0] = true;
            }else{
                this.style.borderColor = 'red';
                 that.arr[0] = false;
            }
        }
        this.ps.onblur = function(){
            var re = /^\w{6,20}/;
            var ps = this.value;
            if(re.test(ps)){
                this.style.borderColor = '';
                 that.arr[1] = true;
            }else{
                this.style.borderColor = 'red';
                 that.arr[1] = false;
            }
        }
       this.ipt_1.onclick = function(){
            if(that.arr.indexOf(false) === -1){
                //用户名
                let us = that.us.value;
                //密码
                let ps = that.ps.value;
                //cookie
                let cookie = new Cookie();
                let cookie_str = cookie.getCookie('registers') ? cookie.getCookie('registers') : '';
                let cookie_obj = that.convertStrToObj(cookie_str);
                if(us in cookie_obj){
                    //如果用户名存在，则判断密码是否正确
                    if(ps == cookie_obj[us]){
                        alert('密码正确');
                        location.href = '../index.html';
                        
                    }else{
                        alert('密码错误');
                    }
                }else{
                    alert('用户名不存在！');
                }
            }else{
                alert('请完善信息!');
            }
        }


        this.key1.onclick = function(){
            key1.style.color = '#767676';
            key2.style.color = '';
            key3.style.color = '';
            div1.style.display = 'block';
            div2.style.display = 'none';
            div3.style.display = 'none';

        }
        this.key2.onclick = function(){
            key2.style.color = '#767676';
            key1.style.color = '';
            key3.style.color = '';
            div1.style.display = 'none';
            div2.style.display = 'block';
            div3.style.display = 'none';

            

        }
        this.key3.onclick = function(){
            key3.style.color = '#767676';
            key1.style.color = '';
            key2.style.color = '';
            div1.style.display = 'none';
            div2.style.display = 'none';
            div3.style.display = 'block';
        }
    }
    $(selector){
        return document.querySelector(selector);
    }
    convertStrToObj(str){
        if(!str){
            return {};
        }
        return JSON.parse(str);
    }
}
new login();
    

